package org.chinarjy.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.chinarjy.api.PersonApi;
import org.chinarjy.dto.PersonInfo;
import org.chinarjy.dto.ResultApi;
import org.chinarjy.entity.Person;
import org.chinarjy.mapper.PersonMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@DubboService
@Component
public class personService implements PersonApi {
    @Autowired
    PersonMapper personMapper;

    //*****************************select操作*************************
    @Override
    public ResultApi<List<PersonInfo>> getAllPersons() {
        log.info("MyService -->> getAllPersons");
        List<PersonInfo> personInfos = new ArrayList<>();
        try {
            List<Person> persons = personMapper.selectList(null);//Mybatis-Plus查询全部，返回对象的List
            for (Person person:persons) {
                personInfos.add(new PersonInfo(person.getId(),person.getName(),person.getAge(),person.getEmail()));
            }
            return ResultApi.success(personInfos);
        }catch (Exception e){
            log.error(e.getMessage());
            return ResultApi.error("1","网络繁忙，稍后再试......");
        }
    }
    @Override
    public ResultApi<PersonInfo> getPersonById(long id){
        log.info("MyService -->> getPersonById");
        Person person = personMapper.selectById(id);
        PersonInfo personInfo = new PersonInfo(person.getId(),person.getName(),person.getAge(),person.getEmail());//根据Id查询，返回对象,没有查询到对象则返回null

        return ResultApi.success(personInfo);
    }
    @Override
    public ResultApi<PersonInfo> getPersonByEmail(String email){//通过唯一字段email查找对象，返回person对象或null
        QueryWrapper<Person> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email",email);
        Person person = personMapper.selectOne(queryWrapper);//根据条件查询一条记录，返回一个对象，如果查询结果超过一条，会报错
        return ResultApi.success(new PersonInfo(person.getId(),person.getName(),person.getAge(),person.getEmail()));
    }
    @Override
    public ResultApi<List<PersonInfo>> getPersonByName(String name){
        List<PersonInfo> personInfos = new ArrayList<>();
        QueryWrapper<Person> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",name);
        List<Person> persons = personMapper.selectList(queryWrapper);
        for(Person person:persons){
            personInfos.add(new PersonInfo(person.getId(),person.getName(),person.getAge(),person.getEmail()));
        }
        return ResultApi.success(personInfos);
    }

    //*****************************insert操作*************************
    @Override
    public ResultApi<PersonInfo> addPerson(PersonInfo personInfo) {
        log.info("MyService -->> addPerson:"+personInfo);
        Person person = new Person(personInfo.getId(),personInfo.getName(),personInfo.getAge(),personInfo.getEmail());
        try {
            personMapper.insert(person);
            return ResultApi.success(personInfo);
        }catch (Exception e){
            log.error(e.getMessage());
            return ResultApi.error("1","网络繁忙，稍后再试......");
        }
    }

    //*****************************delete操作*************************
    @Override
    public ResultApi<PersonInfo> removePerson(PersonInfo personInfo) {
        log.info("MyService -->> addPerson:"+personInfo);
        Map<String,Object> map = new HashMap<>();
        map.put("name",personInfo.getName());
        try {
            personMapper.deleteByMap(map);
            return ResultApi.success(personInfo);
        }catch (Exception e){
            log.error(e.getMessage());
            return ResultApi.error("1","网络繁忙，稍后再试......");
        }
    }

    //*****************************update操作*************************
    @Override
    public ResultApi<PersonInfo> updatePerson(PersonInfo personInfo) {
        log.info("MyService -->> updatePerson");
        Person person = new Person(personInfo.getId(),personInfo.getName(),personInfo.getAge(),personInfo.getEmail());
        try {
            personMapper.updateById(person);
            return ResultApi.success(personInfo);
        }catch (Exception e){
            log.error(e.getMessage());
            return ResultApi.error("1","网络繁忙，稍后再试......");
        }
    }
}
